package com.playwright.util.course;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.playwright.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author liyanhao
 * @Date 2024-01-04 9:26
 */
public class HubstudioDemo {
    public static void main(String[] args) throws InterruptedException {
        //发起post请求获得debuggingPort
        try {
            URL url = new URL("http://127.0.0.1:6873/api/v1/browser/start");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json");
            String requestBody = "{\"containerCode\": \"421408399\"}";
            conn.getOutputStream().write(requestBody.getBytes());
            StringBuilder body = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                body.append(line);
            }
            br.close();
            // Get response
            int responseCode = conn.getResponseCode();
            System.out.println("Response Code: " + responseCode);
            if (responseCode != 200) {
                throw new RuntimeException("获取参数失败");
            }
            ObjectMapper objectMapper = new ObjectMapper();
            System.out.println(body);
            HubstudioResponse hubstudioResponse = objectMapper.readValue(body.toString(), HubstudioResponse.class);
            String debuggingPort = hubstudioResponse.getData().getDebuggingPort();

            new Thread(() -> {
                try {
                    extracted(debuggingPort);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }).start();

        } catch (Exception e) {
            e.printStackTrace();
        }




/*
        new Thread(() -> {
            try {
                extracted("49423");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();


        new Thread(() -> {
            try {
                extracted("49291");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();*/
    }

    private static void extracted(String id) throws InterruptedException {
        // 获取playwright浏览器会话
        Playwright playwright = Playwright.create();
        BrowserType chromium = playwright.chromium();
        Browser browser = chromium.connectOverCDP("http://127.0.0.1:" + id); // 填写环境的debuggingPort参数
        BrowserContext browserContext = browser.contexts().get(0);
        // 运行脚本
        // 使用第一个标签页打开playwright官网
        Page page = browserContext.pages().get(0);
        //监听竞价商品接口  https://mms.pinduoduo.com/lakemms/bid/query/bidList
        page.navigate("https://mms.pinduoduo.com/act-bidding/market-sign-list?activity_status=ALL");

        page.route("https://mms.pinduoduo.com/lakemms/bid/query/bidList", route -> {
            byte[] body = route.request().response().body();
            try {
                System.out.println(new String(body, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }

        });
        //跳转到竞价页面
        page.navigate("https://mms.pinduoduo.com/act-bidding/market-sign-list?activity_status=ALL");
        System.out.println("打开竞价信息页面成功");
        //点击活动中按钮
        page.getByText("活动中").first().click();
        System.out.println("执行完完毕");
        Thread.sleep(100000l);
        page.waitForLoadState();
    }



}

